<?php
if (!defined('NV_SYSTEM')) { die ("You can't access this file directly..."); }//nukeviet120606
if ($popup != "1"){
    $module_name = basename(dirname(__FILE__));
    require("modules/".$module_name."/nukebb.php");
}
else
{
    $phpbb_root_path = 'modules/Forums/';
}

define('IN_PHPBB', true);
// ############ Edit below ############
$length = '45'; // length of topic title
$special_forums = '0'; // 0 = no; 1 = yes
$forums = '1,2,3'; // IDs of forums; separate them with a comma
// ############ Edit above ############

include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

$userdata = session_pagestart($user_ip, PAGE_INDEX, $board_config['session_length']);
init_userprefs($userdata);

$page_title = $lang['latest_posts'];
include('includes/page_header.'.$phpEx);

$start = ( isset($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
if (!isset($selorder)) $selorder = "las24";
if (!isset($nodays)) $nodays = 3;

$sql = "SELECT post_id FROM " . POSTS_TABLE;
if ( !($result = $db->sql_query($sql)) )
{
	message_die(GENERAL_ERROR, 'Could not obtain matched posts list', '', __LINE__, __FILE__, $sql);
}
$recent_ids = array();
while( $row = $db->sql_fetchrow($result) )
{
	$recent_ids[] = $row['post_id'];
}
$db->sql_freeresult($result);

$recent_forum = ( isset($HTTP_POST_VARS['forum']) ) ? intval($HTTP_POST_VARS['forum']) : -1;
if ( $recent_forum == -1 )
{
        $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata); 
        $ignore_forum_sql = '';
        while( list($key, $value) = each($is_auth_ary) )
        {
                if ( !$value['auth_read'] )
                {
                        $ignore_forum_sql .= ( ( $ignore_forum_sql != '' ) ? ', ' : '' ) . $key;
                }
        }
        if ( $ignore_forum_sql != '' )
        {
                $auth_sql .= ( $auth_sql != '' ) ? " AND f.forum_id NOT IN ($ignore_forum_sql) " : "f.forum_id NOT IN ($ignore_forum_sql) ";
        }
}
                if ( $recent_author == '' && $auth_sql == '' )
                {
                        $sql = "SELECT topic_id FROM " . POSTS_TABLE . "
                                WHERE post_id IN (" . implode(", ", $recent_ids) . ") 
                                $where_sql GROUP BY topic_id";
                }
                else
                {
                        $from_sql = POSTS_TABLE . " p"; 
                        if ( $auth_sql != '' )
                        {
                                $from_sql .= ", " . FORUMS_TABLE . " f";
                                $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
                        }
                        $sql = "SELECT p.topic_id FROM $from_sql 
                                WHERE p.post_id IN (" . implode(", ", $recent_ids) . ") 
                                $where_sql GROUP BY p.topic_id";
                }
                if ( !($result = $db->sql_query($sql)) )
                {
                        message_die(GENERAL_ERROR, 'Could not obtain topic ids', '', __LINE__, __FILE__, $sql);
                }

                $recent_ids = array();
                while( $row = $db->sql_fetchrow($result) )
                {
                        $recent_ids[] = $row['topic_id'];
                }
                $db->sql_freeresult($result);

$auth_results = implode(', ', $recent_ids);
$where_part1 = ( $special_forums == '0' ) ? 't.topic_id IN ('.$auth_results.')' : 'f.forum_id IN ('.$forums.')';

$part1 = "SELECT from_unixtime(p.post_time, '%d.%m.%y %H:%i:%s') as topic_time, f.forum_name as forum_name, f.forum_id as forum_id,
                        u.username as username, u2.username as username2, u2.user_id as user_id2, p.poster_id as poster_id, p.post_username as post_username, p.post_id as post_id, t.*, p.post_time as post_time
               FROM ".TOPICS_TABLE." t, ".POSTS_TABLE." p
                        LEFT OUTER JOIN ".FORUMS_TABLE." f on p.forum_id = f.forum_id
                        LEFT OUTER JOIN ".USERS_TABLE." u on p.poster_id = u.user_id
                        LEFT OUTER JOIN ".USERS_TABLE." u2 on u2.user_id = t.topic_poster
               WHERE $where_part1 AND p.post_id = t.topic_last_post_id AND ";
$part2 = " ORDER BY p.post_time desc LIMIT $start, ".$board_config['topics_per_page'];

$showdays = 86400 * $nodays;

$lastweeksql  = $part1."UNIX_TIMESTAMP(NOW()) - p.post_time < 691200".$part2;
$yesterdaysql = $part1."FROM_UNIXTIME(p.post_time,'%Y%m%d') - FROM_UNIXTIME(unix_timestamp(NOW()),'%Y%m%d') = -1".$part2;
$todaysql     = $part1."FROM_UNIXTIME(p.post_time,'%Y%m%d') - FROM_UNIXTIME(unix_timestamp(NOW()),'%Y%m%d') = 0".$part2;
$last24hsql   = $part1."UNIX_TIMESTAMP(NOW()) - p.post_time < 86400".$part2;
$lastXdsql    = $part1."UNIX_TIMESTAMP(NOW()) - p.post_time < $showdays".$part2;

switch ($selorder)
{
 case "las24" : $sql = $last24hsql; $template->assign_vars(array("MSG" => $lang['last_24h'])); break;
 case "today" : $sql = $todaysql; $template->assign_vars(array("MSG" => $lang['today'])); break;
 case "yestr" : $sql = $yesterdaysql; $template->assign_vars(array("MSG" => $lang['yesterday'])); break;
 case "lweek" : $sql = $lastweeksql; $template->assign_vars(array("MSG" => $lang['last_week'])); break;
 case "laday" : $sql = $lastXdsql; $template->assign_vars(array("MSG" => $lang['last_xdays1'].$nodays.$lang['last_xdays2'])); break;
}

$style = 0;
if ( !$result = $db->sql_query($sql) )
{
        message_die(GENERAL_ERROR, 'Could not obtain main information', '', __LINE__, __FILE__, $sql);
}
$line = array();
while( $row = $db->sql_fetchrow($result) )
{
        $line[] = $row;
}
$db->sql_freeresult($result);		
		
        $template->set_filenames(array(
                'body' => 'recent_body.tpl')
        );

        $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array();
        $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array();
        for($i = 0; $i < count($line); $i++)
        {
                $forum_url = append_sid("viewforum.$phpEx?" . POST_FORUM_URL . '=' . $line[$i]['forum_id']);
                $forum_id = $line[$i]['forum_id'];
                $topic_url = append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . '=' . $line[$i]['topic_id']);
                $topic_id = $line[$i]['topic_id'];

                $orig_word = array();
                $replacement_word = array();
                obtain_word_list($orig_word, $replacement_word);
                $word_censor = ( count($orig_word) ) ? preg_replace($orig_word, $replacement_word, $line[$i]['topic_title']) : $line[$i]['topic_title']; 
                $topic_title = ( strlen($line[$i]['topic_title']) < $length ) ? $word_censor : substr(stripslashes($word_censor), 0, $length) . "..."; 

                $topic_type = ( $line[$i]['topic_type'] == POST_ANNOUNCE ) ? $lang['Topic_Announcement'] .' ': '';
                $topic_type .= ( $line[$i]['topic_type'] == POST_GLOBAL_ANNOUNCE ) ? $lang['Topic_global_announcement'] .' ': '';
                $topic_type .= ( $line[$i]['topic_type'] == POST_STICKY ) ? $lang['Topic_Sticky'] .' ': '';
                $topic_type .= ( $line[$i]['topic_vote'] ) ? $lang['Topic_Poll'] .' ': '';

                $views = $line[$i]['topic_views'];
                $replies = $line[$i]['topic_replies'];
                if ( ( $replies + 1 ) > $board_config['posts_per_page'] )
                {
                        $total_pages = ceil( ( $replies + 1 ) / $board_config['posts_per_page'] );
                        $goto_page = ' [ ';
                        $times = 1;
                        for($j = 0; $j < $replies + 1; $j += $board_config['posts_per_page'])
                        {
                                $goto_page .= '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=" . $topic_id . "&amp;start=$j") . '" class=genmed>' . $times . '</a>';
                                if ( $times == 1 && $total_pages > 4 )
                                {
                                        $goto_page .= ' ... ';
                                        $times = $total_pages - 3;
                                        $j += ( $total_pages - 4 ) * $board_config['posts_per_page'];
                                }
                                else if ( $times < $total_pages )
                                {
                                        $goto_page .= ', ';
                                }
                                $times++;
                        }
                        $goto_page .= ' ] ';
                }
                else
                {
                        $goto_page = '';
                }

                if ( $line[$i]['topic_status'] == TOPIC_LOCKED )
                {
                        $folder = $images['folder_locked'];
                        $folder_new = $images['folder_locked_new'];
                }
                else if ( $line[$i]['topic_type'] == POST_ANNOUNCE )
                {
                        $folder = $images['folder_announce'];
                        $folder_new = $images['folder_announce_new'];
                }
                else if ( $line[$i]['topic_type'] == POST_GLOBAL_ANNOUNCE )
                {
                        $folder = $images['folder_global_announce'];
                        $folder_new = $images['folder_global_announce_new'];
                }
                else if ( $line[$i]['topic_type'] == POST_STICKY )
                {
                        $folder = $images['folder_sticky'];
                        $folder_new = $images['folder_sticky_new'];
                }
                else
                {
                        if ( $replies >= $board_config['hot_threshold'] )
                        {
                                $folder = $images['folder_hot'];
                                $folder_new = $images['folder_hot_new'];
                        }
                        else
                        {
                                $folder = $images['folder'];
                                $folder_new = $images['folder_new'];
                        }
                }

                if ( $userdata['session_logged_in'] )
                {
                        if ( $line[$i]['post_time'] > $userdata['user_lastvisit'] ) 
                        {
                                if ( !empty($tracking_topics) || !empty($tracking_forums) || isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
                                {
                                        $unread_topics = true;
                                        if ( !empty($tracking_topics[$topic_id]) )
                                        {
                                                if ( $tracking_topics[$topic_id] > $line[$i]['post_time'] )
                                                {
                                                        $unread_topics = false;
                                                }
                                        }
                                        if ( !empty($tracking_forums[$forum_id]) )
                                        {
                                                if ( $tracking_forums[$forum_id] > $line[$i]['post_time'] )
                                                {
                                                        $unread_topics = false;
					}
                                        }
                                        if ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all']) )
                                        {
                                                if ( $HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f_all'] > $line[$i]['post_time'] )
                                                {
                                                        $unread_topics = false;
					}
                                        }
                                        if ( $unread_topics )
                                        {
                                                $folder_image = $folder_new;
					$folder_alt = $lang['New_posts'];
                				$newest_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&amp;view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
                                        }
                                        else
                                        {
                                                $folder_alt = ( $line[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
                                                $folder_image = $folder;
					$folder_alt = $folder_alt;
					$newest_img = '';
                                        }
                                }
                                else if ( $line[$i]['post_time'] > $userdata['user_lastvisit'] ) 
                                {
                                        $folder_image = $folder_new;
                                        $folder_alt = $lang['New_posts'];
                                        $newest_img = '<a href="' . append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$topic_id&amp;view=newest") . '"><img src="' . $images['icon_newest_reply'] . '" alt="' . $lang['View_newest_post'] . '" title="' . $lang['View_newest_post'] . '" border="0" /></a> ';
                                }
                                else 
                                {
                                        $folder_image = $folder;
                                        $folder_alt = ( $line[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
                                        $newest_img = '';
                                }
                        }
                        else
                        {
                                $folder_image = $folder;
                                $folder_alt = ( $line[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
                                $newest_img = '';
                        }
                }
                else
                {
                        $folder_image = $folder;
                        $folder_alt = ( $line[$i]['topic_status'] == TOPIC_LOCKED ) ? $lang['Topic_locked'] : $lang['No_new_posts'];
                        $newest_img = '';
                }
				
                $first_time = create_date($board_config['default_dateformat'], $line[$i]['topic_time'], $board_config['board_timezone']);
                $first_author = ( $line[$i]['user_id2'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '='  . $line[$i]['user_id2']) . '">'.$line[$i]['username2'].'</a>' : ( ($line[$i]['post_username2'] != '' ) ? $line[$i]['post_username2'] : $lang['Guest'] );
                $last_time = create_date($board_config['default_dateformat'], $line[$i]['post_time'], $board_config['board_timezone']);
                $last_author = ( $line[$i]['poster_id'] != ANONYMOUS ) ? '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '='  . $line[$i]['poster_id']) . '">'.$line[$i]['username'].'</a>' : ( ($line[$i]['post_username'] != '' ) ? $line[$i]['post_username'] : $lang['Guest'] );
                $last_url = '<a href="' . append_sid("viewtopic.$phpEx?"  . POST_POST_URL . '=' . $line[$i]['topic_last_post_id']) . '#' . $line[$i]['topic_last_post_id'] . '"><img src="' . $images['icon_latest_reply'] . '" alt="' . $lang['View_latest_post'] . '" title="' . $lang['View_latest_post'] . '" border="0" /></a>';

                $row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

                $template->assign_block_vars('recent', array(
                        'TOPIC_TITLE' => $topic_title,
                        'TOPIC_TYPE' => $topic_type,
                        'VIEWS' => $views,
                        'REPLIES' => $replies,
                        'GOTO_PAGE' => $goto_page,
                        'TOPIC_FOLDER_IMG' => '<img src="'.$folder_image.'" width="19" height="18" alt="'.$folder_alt.'" />', 
                        'NEWEST_IMG' => $newest_img, 
                        'FIRST_TIME' => sprintf($lang['Started'], $first_time),
                        'FIRST_AUTHOR' => sprintf($lang['By'], $first_author),
                        'LAST_TIME' => $last_time,
                        'LAST_AUTHOR' => $last_author,
                        'LAST_URL' => $last_url,
                        'ROW_CLASS' => $row_class,
                        'L_REPLIES' => $lang['Replies'],
                        'L_VIEWS' => $lang['Views'],
                        'FORUM_NAME' => $line[$i]['forum_name'],
                        'U_VIEW_FORUM' => $forum_url, 
                        'FORUM_ID' => $forum_id,
                        'U_VIEW_TOPIC' => $topic_url,
                        'TOPIC_ID' => $topic_id
                ));
        }

if ( $selorder == 'las24' )
{
        $where = "WHERE t.topic_id IN ($auth_results) AND UNIX_TIMESTAMP(NOW()) - p.post_time < 86400";
        $l_selorder = $lang['24h'];
}
else if ( $selorder == 'today' )
{
        $where = "WHERE $where_part1 AND FROM_UNIXTIME(p.post_time,'%Y%m%d') - FROM_UNIXTIME(unix_timestamp(NOW()),'%Y%m%d') = 0";
        $l_selorder = $lang['tday'];
}
else if ( $selorder == 'yestr' )
{
        $where = "WHERE $where_part1 AND FROM_UNIXTIME(p.post_time,'%Y%m%d') - FROM_UNIXTIME(unix_timestamp(NOW()),'%Y%m%d') = -1";
        $l_selorder = $lang['yday'];
}
else if ( $selorder == 'lweek' )
{
        $where = "WHERE $where_part1 AND UNIX_TIMESTAMP(NOW()) - p.post_time < 691200";
        $l_selorder = $lang['week'];
}
else if ( $selorder == 'laday' )
{
        $where = "WHERE $where_part1 AND UNIX_TIMESTAMP(NOW()) - p.post_time < $showdays";
        $l_selorder = sprintf($lang['xdays'],$nodays);
}

$select_special = ( $special_forums == '0' ) ? '' : ' , '.FORUMS_TABLE.' f';
$where_special = ( $special_forums == '0' ) ? '' : ' AND t.forum_id = f.forum_id';
$sql = "SELECT count(*) AS total FROM ".TOPICS_TABLE." t , ".POSTS_TABLE." p $select_special $where AND p.post_id = t.topic_last_post_id $where_special";

if ( !($result = $db->sql_query($sql)) )
{
        message_die(GENERAL_ERROR, 'Error getting total topics', '', __LINE__, __FILE__, $sql);
}
if ( $total = $db->sql_fetchrow($result) )
{
        $total_topics = $total['total'];
        $pagination = generate_pagination("recent.$phpEx?nodays=$nodays&selorder=$selorder", $total_topics, $board_config['topics_per_page'], $start). '&nbsp;';
}

$template->assign_vars(array(
        'L_RECENT_TITLE' => sprintf($lang['Title'],$total_topics,$l_selorder),
        'L_TOPIC' => $lang['Topic'],
        'L_MATCHES' => $lang['matches'],
        'L_LAS24_MSG' => $lang['last_24h'],
        'L_TODAY_MSG' => $lang['today'],
        'L_YESTR_MSG' => $lang['yesterday'],
        'L_LWEEK_MSG' => $lang['last_week'],
        'L_LADAY_MSG' => $lang['last_xdays'],
        'L_SHOW' => $lang['show_posts'],
        'L_SHOWING' => $lang['showing_posts'],
        'L_DAYS' => $lang['day_posts'],
        'L_LAST' => $lang['last_posts'],
        'L_GOTO_PAGE' => $lang['Goto_page'],
        'NODAYS' => $nodays,
        'FORM_ACTION' =>  append_sid("recent.$phpEx"),
        'PAGINATION' => $pagination,
        'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $total_topics / $board_config['topics_per_page'] )), 
));

$template->pparse('body');
include('includes/page_tail.'.$phpEx);
?>